//
//  AFNetworkTool.m
//  Test
//
//  Created by 何欣 on 15/9/11.
//  Copyright (c) 2015年 何欣. All rights reserved.
//


#import "AFNetworkTool.h"
@implementation AFNetworkTool
+ (void)getUrl:(NSString *)url
          body:(id)body
        result:(HXResult)result
    headerFile:(NSDictionary *)headerFile
       success:(void (^)(id result))success
       failure:(void (^)(NSError *error))failure
{
    NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    // 1.获取网络请求管理类
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2.为网络请求加请求头
    if (headerFile) {
        for (NSString *key in headerFile.allKeys) {
            [manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];
        }
    }
    // 3.设置返回值类型
    switch (result) {
        case HXData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case HXJSON:
            break;
        case HXXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    // 4.设置网络请求返回值所支持的参数类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil]];
    // 让网络指示器旋转
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    manager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    // 5.发送网络请求
    [manager GET:urlStr parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            // 成功回调
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}


+ (void)postUrl:(NSString *)url
           body:(id)body
         result:(HXResult)result
   requestStyle:(HXRequestStyle)requestStyle
     headerFile:(NSDictionary *)headerFile
        success:(void (^)(id result))success
        failure:(void (^)(NSError *error))failure
{
    // 1.获取网络请求管理类
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.网络请求请求体Body的类型
    switch (requestStyle) {
        case HXRequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case HXRequestString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError *__autoreleasing *error) {
                return parameters;
            }];
            break;
            
        default:
            break;
    }
    
    // 3.为网络请求加请求头
    if (headerFile) {
        for (NSString *key in headerFile.allKeys) {
            [manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];
        }
    }
    // 4.网络请求返回值的数据类型
    switch (result) {
        case HXData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case HXJSON:
            break;
        case HXXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    
    // 5.设置网络请求返回值支持的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain",@"application/x-javascript" ,nil]];
    
    // 6.发送网络请求
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
            
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
            failure(error);
            
        }
        
    }];
    
}

@end
